package com.iweb.web.controller;

import com.iweb.commons.utils.ModelAndView;
import com.iweb.pojo.Emp;
import com.iweb.service.EmpService;

import javax.servlet.http.HttpSession;

/**
 * @author: jack
 * @date: 2022-08-04 0004
 * @ApiNOTE:
 */
public class EmpController {

  private EmpService empService;

  public void setEmpService(EmpService empService) {
    this.empService = empService;
  }

  public ModelAndView login(Emp emp, HttpSession session) {
    ModelAndView mv = new ModelAndView();
    // 根据 账号查询员工
    Emp data = empService.findEmpByAccount(emp.getAccount());

    if (data == null) {
      mv.setViewName("/WEB-INF/views/error.jsp");
      mv.addModel("message", "用户不存在");
      return mv;
    }

    if (!data.getPassword().equals(emp.getPassword())) {
      mv.setViewName("/WEB-INF/views/error.jsp");
      mv.addModel("message", "密码不正确");
      return mv;
    }
    session.setAttribute("emp_id", data.getEid());
    session.setAttribute("emp_name", data.getUsername());
    mv.setViewName("/WEB-INF/views/main.jsp");
    return mv;
  }
}
